/**
 * www.lifexx.com Inc.
 * Copyright (c) 2014 All Rights Reserved.
 */
package net.mikoo.seals.boss.biz.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.mikoo.seals.boss.biz.mapper.boss.RolePermitMapper;
import net.mikoo.seals.boss.biz.services.RolePermitService;
import net.mikoo.seals.boss.common.BaseServices;
import net.mikoo.seals.boss.common.exception.ParametersException;
import net.mikoo.seals.boss.common.helper.MenuHelper;
import net.mikoo.seals.boss.model.Menu;
import net.mikoo.seals.boss.model.RolePermit;

/**
 * 
 * @Filename RolePermitServiceImpl.java
 *
 * @Description
 *
 * @Version 1.0
 *
 * @Author chenzx
 *
 * @Email chenzx1206@163.com
 * 
 * @History
 *          <li>Author: chenzx</li>
 *          <li>Date: 2015年1月27日</li>
 *          <li>Version: 1.0</li>
 *          <li>Content: create</li>
 *
 */
@Service
public class RolePermitServiceImpl extends BaseServices implements RolePermitService {

	Logger logger = Logger.getLogger(RolePermitServiceImpl.class);

	@Autowired
	private RolePermitMapper rolePermitMapper;

	@Override
	public List<RolePermit> loadByPrimaryKey(Long roleId) {
		List<RolePermit> rolePermits = rolePermitMapper.selectByRoleId(roleId);
		return rolePermits;
	}

	@Override
	public void deleteByPrimaryKey(Long roleId) {

		if (roleId == null) {
			throw new ParametersException("roleId is null!");
		}
		// 打印执行参数日志
		rolePermitMapper.deleteByRoleId(roleId);

	}

	private List<Menu> convertToMenus(Long roleId) {
		List<Menu> menus = new ArrayList<Menu>();
		List<RolePermit> rms = rolePermitMapper.selectByRoleId(roleId);
		if (rms != null && rms.size() > 0) {
			for (RolePermit rm : rms) {
				menus.add(rm.getMenu());
			}
		}
		return menus;
	}

	private List<RolePermit> convertToRolePermits(List<Menu> menus, Long roleId) {
		List<RolePermit> rms = rolePermitMapper.selectByRoleId(roleId);
		if (rms.size() == menus.size()) {
			for (int i = 0; i < menus.size(); i++) {
				RolePermit rm = rms.get(i);
				Menu menu = menus.get(i);
				rm.setMenu(menu);
			}
		}
		return rms;
	}

	@Override
	public List<RolePermit> loadAsTree(Long roleId) {

		List<Menu> menus = new ArrayList<Menu>();
		List<Menu> childs = convertToMenus(roleId);
		menus = MenuHelper.convertToTree(childs, 0L, 0);

		List<RolePermit> rms = convertToRolePermits(menus, roleId);
		// 打印执行成功日志
		return rms;
	}

	@Override
	public void updateByRoleIdAndMenuId(Long roleId, String[] menuIds) {
		
			if(roleId == null) {
				throw new ParametersException("roleId is null!");
			}
			if(menuIds == null) {
				throw new ParametersException("menuIds is null!");
			}
			// 打印执行参数日志
			
			// 设置改角色下的所有菜单权限为不可用
			rolePermitMapper.updateByRoleId(roleId);
			
			List<RolePermit> rolePermits = new ArrayList<RolePermit>();
			for(String menuId : menuIds) {
				RolePermit rolePermit = new RolePermit();
				rolePermit.setRoleId(roleId);
				rolePermit.setMenuId(Long.valueOf(menuId));
				//rolePermit.setStatus(1);
				rolePermitMapper.updateByRoleIdAndMenuId(rolePermit);
				rolePermits.add(rolePermit);
			}
			
			
	}

	/**
	 * @param partyId
	 * @return
	 * @see cn.shopstart.shop.boss.system.permit.service.RolePermitService#loadHaveResource(java.lang.Long)
	 */
	@Override
	public List<Menu> loadHaveResource(Long partyId) {

		List<Menu> menus = rolePermitMapper.selectHaveResource(partyId);

		return menus;
	}
	
	@Override
	public List<Menu> loadHaveResourceAsTree(Long partyId) {

		List<Menu> menus = rolePermitMapper.selectHaveResource(partyId);
		menus = MenuHelper.convertToTree(menus, 0L);
		return menus;
	}

	@Override
	public List<Menu> loadRootResource(Long partyId) {
		return rolePermitMapper.selectRootResource(partyId);
	}

	@Override
	public List<Menu> loadSubResource(Long partyId, Long parentId) {

		List<Menu> menus = rolePermitMapper.loadSubResource(partyId, parentId);

		if (menus != null && !menus.isEmpty()) {
			for (Menu menu : menus) {
				List<Menu> childs = rolePermitMapper.loadSubResource(partyId, menu.getMenuId());
				menu.setChilds(childs);
			}
		}

		return menus;
	}

	@Override
	public List<Menu> loadResourceByRoleId(Long roleId) {

		if (roleId == null) {
			throw new ParametersException("roleId is null!");
		}

		List<Menu> menus = rolePermitMapper.selectResourceByRoleId(roleId);

		return menus;
	}
}
